package com.dds.system.service.union.impl;

import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.dds.common.core.redis.RedisCache;
import com.dds.common.utils.DateUtils;
import com.dds.system.domain.union.UnionShopSubsidyPeriod;
import com.dds.system.mapper.union.UnionShopSubsidyPeriodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.union.UnionShopSubsidyMapper;
import com.dds.system.domain.union.UnionShopSubsidy;
import com.dds.system.service.union.IUnionShopSubsidyService;

import javax.validation.constraints.Null;

/**
 * 商家特殊补贴Service业务层处理
 *
 * @author gaolianjie
 * @date 2021-04-16
 */
@Service
public class UnionShopSubsidyServiceImpl implements IUnionShopSubsidyService {
    @Autowired
    private UnionShopSubsidyMapper unionShopSubsidyMapper;

    @Autowired
    private UnionShopSubsidyPeriodMapper unionShopSubsidyPeriodMapper;

    @Autowired
    private UnionShopSubsidyPeriodServiceImpl unionShopSubsidyPeriodService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询商家特殊补贴
     *
     * @param id 商家特殊补贴ID
     * @return 商家特殊补贴
     */
    @Override
    public Map<String, Object> selectUnionShopSubsidyById(Integer id) {
        Map<String, Object> data = new HashMap<>();
        UnionShopSubsidy unionShopSubsidy = unionShopSubsidyMapper.selectUnionShopSubsidyById(id);
        Map subsidy = JSON.parseObject(JSON.toJSONString(unionShopSubsidy), Map.class);
        data.putAll(subsidy);

        List<UnionShopSubsidyPeriod> unionShopSubsidyPeriods = unionShopSubsidyPeriodMapper.selectUnionShopSubsidyPeriodList(unionShopSubsidy.getShopId());

        List<Map<String, Object>> periods = new ArrayList<>();
        for (UnionShopSubsidyPeriod period : unionShopSubsidyPeriods) {
            String start = period.getPeriodStart() / 60 + ":" + period.getPeriodStart() % 60;
            String end = period.getPeriodEnd() / 60 + ":" + period.getPeriodEnd() % 60;

            Map<String, Object> pData = new HashMap<>();

            List<String> periodTime = new ArrayList<>();
            periodTime.add(start);
            periodTime.add(end);

            pData.put("periodTime", periodTime);
            pData.put("subsidyRatioMin", period.getSubsidyRatioMin().multiply(BigDecimal.valueOf(100)));
            pData.put("subsidyRatioMax", period.getSubsidyRatioMax().multiply(BigDecimal.valueOf(100)));

            periods.add(pData);
            
        }

        data.put("period", periods);

        return data;
    }

    /**
     * 查询商家特殊补贴列表
     *
     * @param shopName 商家名称
     * @return 商家特殊补贴
     */
    @Override
    public List<Map<String, Object>> selectUnionShopSubsidyList(String shopName) {
        List<Map<String, Object>> lst = unionShopSubsidyMapper.selectUnionShopSubsidyList(shopName);

        for (int i = 0; i < lst.size(); i++) {
            Map<String, Object> info = lst.get(i);
            String redisKey = info.get("shop_id").toString() + DateUtils.getDate().replaceAll("-", "");
            info.put("nowDayCount", redisCache.getCacheObject(redisKey) != null ? redisCache.getCacheObject(redisKey) : "0");
        }

        return lst;
    }

    /**
     * 新增商家特殊补贴
     *
     * @param params 商家特殊补贴
     * @param userId 当前登录用户id
     * @return 结果
     */
    @Override
    public int insertUnionShopSubsidy(Map<String, Object> params, Long userId) {
        UnionShopSubsidy subsidy = UnionShopSubsidy.of()
                .setShopId(params.get("shopId").toString())
                .setMinOrderAmount(new BigDecimal(params.get("minOrderAmount").toString()))
                .setMaxOrderAmount(new BigDecimal(params.get("maxOrderAmount").toString()))
                .setBaseSubsidyRatio(new BigDecimal(params.get("baseSubsidyRatio").toString()))
                .setMaxCountDaily(Long.valueOf(params.get("maxCountDaily").toString()))
                .setProbability(Integer.valueOf(params.get("probability").toString()))
                .setCreateTime(new Date())
                .setCreateUser(userId);

        int period = unionShopSubsidyPeriodService.insertUnionShopSubsidyPeriod(
                (List<Map<String, Object>>) params.get("period"),
                subsidy.getShopId()
        );

        if (period > 0) {
            return unionShopSubsidyMapper.insertUnionShopSubsidy(subsidy);
        }

        return 0;
    }

    /**
     * 修改商家特殊补贴
     *
     * @param params 商家特殊补贴
     * @param userId 当前登录用户id
     * @return 结果
     */
    @Override
    public int updateUnionShopSubsidy(Map<String, Object> params, Long userId) {
        UnionShopSubsidy subsidy = UnionShopSubsidy.of()
                .setId(Integer.valueOf(params.get("id").toString()))
                .setShopId(params.get("shopId").toString())
                .setMinOrderAmount(new BigDecimal(params.get("minOrderAmount").toString()))
                .setMaxOrderAmount(new BigDecimal(params.get("maxOrderAmount").toString()))
                .setBaseSubsidyRatio(new BigDecimal(params.get("baseSubsidyRatio").toString()))
                .setMaxCountDaily(Long.valueOf(params.get("maxCountDaily").toString()))
                .setProbability(Integer.valueOf(params.get("probability").toString()))
                .setCreateTime(new Date())
                .setCreateUser(userId);

        unionShopSubsidyPeriodMapper.deleteUnionShopSubsidyPeriodByShopId(subsidy.getShopId());

        int period = unionShopSubsidyPeriodService.insertUnionShopSubsidyPeriod(
                (List<Map<String, Object>>) params.get("period"),
                subsidy.getShopId()
        );

        if (period > 0) {
            return unionShopSubsidyMapper.updateUnionShopSubsidy(subsidy);
        }

        return 0;
    }

    /**
     * 批量删除商家特殊补贴
     *
     * @param ids 需要删除的商家特殊补贴ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopSubsidyByIds(Integer[] ids) {
        for (Integer i : ids) {
            UnionShopSubsidy unionShopSubsidy = unionShopSubsidyMapper.selectUnionShopSubsidyById(i);

            unionShopSubsidyPeriodMapper.deleteUnionShopSubsidyPeriodByShopId(unionShopSubsidy.getShopId());
        }

        return unionShopSubsidyMapper.deleteUnionShopSubsidyByIds(ids);
    }

    /**
     * 删除商家特殊补贴信息
     *
     * @param id 商家特殊补贴ID
     * @return 结果
     */
    @Override
    public int deleteUnionShopSubsidyById(Integer id) {
        return unionShopSubsidyMapper.deleteUnionShopSubsidyById(id);
    }
}
